//
//  EditPasdController.m
//  LiquorChain
//
//  Created by apple on 2019/1/2.
//  Copyright © 2019年 王冬. All rights reserved.
//

#import "EditPasdController.h"

@interface EditPasdController ()
///新密码
@property (nonatomic,strong)UITextField *passwordTextField;
/// 短信验证码
@property (nonatomic,strong)UITextField *verificationTextField;
///获取短信验证码
@property (nonatomic,strong) UIButton *verificationBtn;
/// 提交
@property (nonatomic,strong) UIButton *commitBtn;
@end

@implementation EditPasdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改登录密码";
    
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, kNavBarHeight, SCREEN_WIDTH, 15)];
    backView.backgroundColor =BACKGROUNDCOLOR;
    [self.view addSubview:backView];
    
    UIView *bodyView =[[UIView alloc]init];
    [self.view addSubview:bodyView];
    bodyView.backgroundColor =[UIColor whiteColor];
    [bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(backView.mas_bottom).with.offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(400);
    }];
    UILabel *titleLabel =[[UILabel alloc]init];
    [bodyView addSubview:titleLabel];
    titleLabel.text = @"已发送短信验证码到";
    titleLabel.textColor =[UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font =[UIFont systemFontOfSize:14];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.left.mas_equalTo(120);
        make.right.mas_equalTo(-120);
        make.height.mas_equalTo(14);
    }];
    UILabel *phoneLabel =[[UILabel alloc]init];
    [bodyView addSubview:phoneLabel];
    phoneLabel.textAlignment = NSTextAlignmentCenter;
    phoneLabel.font =[UIFont systemFontOfSize:24];
    phoneLabel.text = @"189****1234";
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).with.offset(14);
        make.left.mas_equalTo(115);
        make.right.mas_equalTo(-115);
        make.height.mas_equalTo(18);
    }];
    self.verificationTextField =[[UITextField alloc]init];
    [bodyView addSubview:self.verificationTextField];
    self.verificationTextField.placeholder = @"请输入验证码";
    self.verificationTextField.font =[UIFont systemFontOfSize:15];
    [self.verificationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneLabel.mas_bottom).with.offset(50);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-120);
        make.height.mas_equalTo(40);
    }];
    self.verificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bodyView addSubview:self.verificationBtn];
    [self.verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.verificationBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.verificationBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.verificationBtn addTarget:self action:@selector(verificationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.verificationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.verificationTextField.mas_right).with.offset(1);
        make.top.mas_equalTo(phoneLabel.mas_bottom).with.offset(50);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    self.passwordTextField =[[UITextField alloc]init];
    [bodyView addSubview:self.passwordTextField];
    self.passwordTextField.placeholder = @"请输入新密码";
    self.passwordTextField.font =[UIFont systemFontOfSize:15];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.verificationTextField.mas_bottom).with.offset(10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    self.commitBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [bodyView addSubview:self.commitBtn];
    [self.commitBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.commitBtn.backgroundColor = CUSTOMCOLOR;
    self.commitBtn.layer.cornerRadius = 5;
    [self.commitBtn addTarget:self action:@selector(commitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.passwordTextField.mas_bottom).with.offset(40);
        make.height.mas_equalTo(50);
    }];
    // Do any additional setup after loading the view.
}

-(void)commitClick:(UIButton *)sender{
    NSLog(@"新密码修改");
}
-(void)verificationAction:(UIButton *)sender{
    
    //获取验证码
    
    __block NSInteger time = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [self.verificationBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.verificationBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                self.verificationBtn.userInteractionEnabled = YES;
            });
            
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.verificationBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                [self.verificationBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                self.verificationBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
